package main

import (
	"fmt"
	"log"
	"os"
	"time"
)

const (
	Cluster           = 0
	Debug             = 0
	NormalStart       = 0 //是否正常启动
	KafkaIpAddress    = "10.10.9.20:9092"
	Cluster0IpAddress = "10.10.9.47"
	Cluster1IpAddress = "10.10.9.48"
	Cluster2IpAddress = "10.10.9.49"
	Cluster3IpAddress = "10.10.9.5"
	ClusterPort       = 8080
	KafkaPort         = ":9091"
	//KafkaIpAddress = "192.168.1.125:9092"
)

func ClusterIp(cluster int) (IP string) {
	switch cluster {
	case 0:
		IP = Cluster0IpAddress
	case 1:
		IP = Cluster1IpAddress
	case 2:
		IP = Cluster2IpAddress
	case 3:
		IP = Cluster3IpAddress
	default:
		log.Println("cluster", Cluster, ":", "error when read obj: Cluster Number Error")
		return
	}
	return IP
}

func main() {
	file, err := os.OpenFile("./output.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
	if err != nil {
		fmt.Println("open log fileed, err", err)
	}
	log.SetOutput(file)

	var a chan Repair
	a = make(chan Repair, 1000)
	// b = make(chan Repair, 1000)
	// c = make(chan Repair, 1000)
	// d = make(chan Repair, 1000)

	log.Println("cluster", Cluster, ":", "启动服务")
	if 1 == 1 {
		go tls_server(0, a)
		go tls_server(1, a)
		go tls_server(2, a)
		go tls_server(3, a)
	} else {
		go InitsServer()
	}
	cli := tls_client()
	log.Println("cluster", Cluster, ":", "等待同步")
	log.Println("cluster", Cluster, ":", Cluster, "开始")

	go test(a, cli)

	for i := 0; i < 4; i++ {

		time.Sleep(1 * time.Second)
	}

	for i := 20; i < 25; i++ {
		go Check(cli, i)
		log.Println("cluster", Cluster, ":", Cluster, "开始抽查区块", i)
		time.Sleep(5 * time.Second)
	}

	for i := 0; i < 3; i++ {
		time.Sleep(3 * time.Second)

	}

}
